<template>
  <div class="body">
    <div class="login">
      <div class="great">
        <div class="small">
          <img src="../assets/logo.png" alt="" />
        </div>
      </div>
      <!-- 表单 -->
      <el-form
        :model="ruleForm"
        label-width="55px"
        class="demo-ruleForm"
        :rules="rules"
        ref="ruleForm"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            v-model="ruleForm.username"
            prefix-icon="iconfont icon-yidongduanicon-"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="ruleForm.password"
            prefix-icon="iconfont icon-mima"
            type="password"
          ></el-input>
        </el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
        <el-button type="info">重置</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 18,
            message: "长度在 6 到 18 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },

  mounted() {},

  methods: {
    login() {
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) {
          return this.$message("用户信息填写不正确");
        } else {
          this.$http.post("login", this.ruleForm).then((res) => {
            console.log(res);
            if (res.data.meta.status === 200) {
              this.$message.success(res.data.meta.msg);
              sessionStorage.setItem("token", res.data.data.token);
              this.$router.push("/Home");
            }
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.body {
  background-color: #2b4b6b;
  height: 100vh;
  .login {
    width: 600px;
    height: 400px;
    background-color: #ffffff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .great {
      width: 130px;
      height: 130px;
      border-radius: 50%;
      background-color: #ffffff;
      box-shadow: 0px 0px 10px gray;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      .small {
        width: 110px;
        height: 110px;
        border-radius: 50%;
        background-color: #eeeeee;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        img {
          width: 100%;
          height: 100%;
          border-radius: 50%;
        }
      }
    }
    .el-form {
      width: 80%;
      height: 80%;
      position: absolute;
      top: 60%;
      left: 50%;
      transform: translate(-50%, -50%);
      text-align: center;
      .el-form-item {
        margin-top: 30px;
      }
    }
  }
}
</style>